
package cn.snmp.mutual.adapter.impl;

import cn.snmp.mutual.nms.SnmpString;
import cn.snmp.mutual.nms.SnmpVar;
import cn.snmp.mutual.adapter.GeneralMibNodeAdapter;
import cn.snmp.mutual.data.ObjService;
import cn.snmp.mutual.util.Utilities;

/**
 * @author dengkn
 * MIB NODE Adapter ，extends GeneralMibNodeAdapter
 * 在读取和写入snmp mib node 的时候实现readMibData、writeMibData方法
 * MacAddrMibNodeAdapter MAC解析
 */
public class MacAddrMibNodeAdapter extends GeneralMibNodeAdapter {

    public String readMibData(SnmpVar var, ObjService mibnode) {
        if (var instanceof SnmpString) {
            String macaddr = ((SnmpString) var).toByteString();
            macaddr = macaddr.trim();
            macaddr = macaddr.replace(' ', ':');
            return macaddr;
        } else
            return "error MacAddress";
    }

    /**
     * @param value
     * @param mibnode
     * @return
     * @see
     */
    public SnmpVar writeMibData(String value, ObjService mibnode) {
        String[] lists = Utilities.StringSplit(value, ':');
        byte[] bytes = new byte[lists.length];
        for (int i = 0; i < lists.length; i++) {
            bytes[i] = (byte) Integer.parseInt(lists[i].trim(), 16);
        }
        SnmpVar var = new SnmpString(bytes);
        return var;
    }

}